package geppetto.phraseHMM.phraseExtraction.extractedphrase.writer;

import geppetto.cat.alignments.AlignmentsSet;
import geppetto.cat.alignments.output.AlignerOutputLatex;
import geppetto.cat.corpus.BilingualCorpus;
import geppetto.phraseHMM.phraseExtraction.PhraseExtractionLaTeXOutput;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePairWriter;
import geppetto.phraseTable.phrase.Phrase;

import java.io.IOException;
import java.io.PrintStream;
import java.util.List;

public class DetailedTableWriter implements ExtractedPhrasePairWriter {
	// output stream/filename
	private PhraseExtractionLaTeXOutput _writer;
	private String _output;
	// bilingual corpus
	private BilingualCorpus _corpus;
	// alignment information
	private AlignmentsSet _alignments;
	private AlignmentsSet _gold;
	// store the current sentence number
	private int _current;
	// sentence number list
	private List<Integer> _sentences;

	public DetailedTableWriter(List<Integer> sentences, String output, BilingualCorpus corpus, AlignmentsSet alignments, AlignmentsSet gold) {
		_sentences = sentences;
		_output = output;
		_corpus = corpus;
		_alignments = alignments;
		_gold = gold;
	}

	public void initialize() {
		_writer = new PhraseExtractionLaTeXOutput(_output);
		// write document begin
		_writer.writeBegin();
		// write word alignments definitions
		AlignerOutputLatex.printWaDef(_writer.getPrintStream());
		AlignerOutputLatex.printAlignmentCaption(_writer.getPrintStream());
		_current = -1;
	}

	public void write(ExtractedPhrasePair epp) {
		try {
			if (_sentences.contains(epp.getSentenceNumber() + 1)) {
				if (_current != epp.getSentenceNumber()) {
					if (_current != -1) {
						// write the end of the previous table
						_writer.writePhrasePairTableEnd("Phrase extraction for sentence number " + (_current + 1));
					}

					// store the current sentence number
					_current = epp.getSentenceNumber();

					String sourceSentence = _corpus.getSourceSentenceString(_current, BilingualCorpus.TRAIN_CORPUS);
					String targetSentence = _corpus.getForeignSentenceString(_current, BilingualCorpus.TRAIN_CORPUS);

					// write page title
					_writer.writePageTitle(_current + 1, sourceSentence, targetSentence);
					// write alignments
					AlignerOutputLatex.outputWithGoldPostLatex(_alignments, _current, _gold, _corpus, _writer.getPrintStream(), 999, "Alignments for sentence number " + (_current + 1));

					_writer.writePhrasePairTableBegin();
				}

				_writer.writePhrasePairTableEntry(Phrase.getSourceString(_corpus, epp.getSourcePhrase()), Phrase.getTargetString(_corpus, epp.getTargetPhrase()),epp.getScore());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeHeaders() {
		// do nothing
	}

	public void close() {
		// write the table end
		_writer.writePhrasePairTableEnd("Phrase extraction for sentence number " + (_current + 1));
		// write the document end
		_writer.writeEnd();
		// close output stream
		_writer.close();
	}

	@Override
	public void writeInfo(String key, String value) {
		// do nothing
	}
}
